package cn.shop.merchant.service;

import cn.hutool.core.util.DesensitizedUtil;
import cn.shop.transcation.bankcard.entity.BankCard;
import cn.shop.transcation.bankcard.mapper.BankCardMapper;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2022-2-16 14:57
 * @description :
 */
@Service
public class BankCardService extends BaseServiceImpl<BankCardMapper, BankCard> {

    public Map<String, Object> getInfo(long id, long ownerId) {
        BankCard card = getOne(Wrappers.<BankCard>lambdaQuery()
                .eq(BankCard::getId, id)
                .eq(BankCard::getOwnerId, ownerId));
        return format(card);
    }

    public boolean updateInfo(long ownerId, BankCard update) {
        return update(update, Wrappers.<BankCard>lambdaQuery()
                .eq(BankCard::getOwnerId, ownerId)
                .eq(BankCard::getId, update.getId()));
    }

    public long getBankCount(long ownerId) {
        return count(Wrappers.<BankCard>lambdaQuery()
                .eq(BankCard::getOwnerId, ownerId));
    }

    public List<Map<String, Object>> bankList(long ownerId) {
        List<BankCard> cards = list(Wrappers.<BankCard>lambdaQuery()
                .eq(BankCard::getOwnerId, ownerId));
        return cards.stream().map(this::format).collect(Collectors.toList());
    }

    private Map<String, Object> format(BankCard card) {
        return Putif.start()
                .anyway("id", card.getId())
                .anyway("bankName", card.getBankName())
                .anyway("cardNo", DesensitizedUtil.bankCard(card.getCardNo()))
                .anyway("name", DesensitizedUtil.chineseName(card.getName()))
                .anyway("mobile", DesensitizedUtil.mobilePhone(card.getMobile()))
                .get();
    }
}
